package com.yangzb.java.sample.xmly;

import cn.hutool.core.util.StrUtil;
import jdk.nashorn.api.scripting.ScriptObjectMirror;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.Arrays;

/**
 * @author yzb
 */
public class UrlParser {
    public double randomSeed;
    public int [] charNumber;
    public String ep;
    public UrlParser(double randomSeed,String charNumber,String ep){
        this.randomSeed = randomSeed;
        this.charNumber = Arrays.asList(charNumber.split("\\*")).stream().mapToInt(Integer::parseInt).toArray();
        this.ep= ep;
    }
    private String allChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\:._-1234567890";
    public double random() {
        randomSeed = (211 * randomSeed + 30031) % 65536;
        return randomSeed/65536;
    }

    public String cgHun(){
        String cgStr = "";
        int length = allChar.length();
        for (int r = 0; r < length; r++) {
            double ran = random();
            double f = ran * allChar.length();
            int n = (int)f;
            cgStr += allChar.charAt(n);
            allChar = allChar.replace(String.valueOf(allChar.charAt(n)),"");
        }
        return cgStr;
    }

    public String decode() throws Exception {
        String cgStr = cgHun();
        String url = "";
        for (int r = 0; r < charNumber.length; r++){
            url += cgStr.charAt(charNumber[r]);
        }
        return url+sign();
    }




    public String sign() throws ScriptException {
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        ScriptEngine engine= scriptEngineManager.getEngineByName("javascript");
        String str = "function n(e, t) {\n" +
                "  return function(e) {\n" +
                "    if (Array.isArray(e))\n" +
                "      return e\n" +
                "  }(e) || function(e, t) {\n" +
                "    var r = null == e ? null : \"undefined\" != typeof Symbol && e[Symbol.iterator] || e[\"@@iterator\"];\n" +
                "    if (null == r)\n" +
                "      return;\n" +
                "    var n, o, i = [], a = !0, u = !1;\n" +
                "    try {\n" +
                "      for (r = r.call(e); !(a = (n = r.next()).done) && (i.push(n.value),\n" +
                "      !t || i.length !== t); a = !0)\n" +
                "        ;\n" +
                "    } catch (e) {\n" +
                "      u = !0,\n" +
                "        o = e\n" +
                "    } finally {\n" +
                "      try {\n" +
                "        a || null == r.return || r.return()\n" +
                "      } finally {\n" +
                "        if (u)\n" +
                "          throw o\n" +
                "      }\n" +
                "    }\n" +
                "    return i\n" +
                "  }(e, t) || function(e, t) {\n" +
                "    if (!e)\n" +
                "      return;\n" +
                "    if (\"string\" == typeof e)\n" +
                "      return o(e, t);\n" +
                "    var r = Object.prototype.toString.call(e).slice(8, -1);\n" +
                "    \"Object\" === r && e.constructor && (r = e.constructor.name);\n" +
                "    if (\"Map\" === r || \"Set\" === r)\n" +
                "      return Array.from(e);\n" +
                "    if (\"Arguments\" === r || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))\n" +
                "      return o(e, t)\n" +
                "  }(e, t) || function() {\n" +
                "    throw new TypeError(\"Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.\")\n" +
                "  }()\n" +
                "}\n" +
                "var a = [19, 1, 4, 7, 30, 14, 28, 8, 24, 17, 6, 35, 34, 16, 9, 10, 13, 22, 32, 29, 31, 21, 18, 3, 2, 23, 25, 27, 11, 20, 5, 15, 12, 0, 33, 26];\n" +
                "var getEncryptedFileParams = function (e) {\n" +
                "  var t = n(function (e, t) {\n" +
                "    var r, n = [], o = 0, i = \"\", a = 0;\n" +
                "    for (n = [],\n" +
                "           o = 0,\n" +
                "           i = \"\",\n" +
                "           a = 0; a < 256; a++)\n" +
                "      n[a] = a;\n" +
                "    for (a = 0; a < 256; a++)\n" +
                "      o = (o + n[a] + e.charCodeAt(a % e.length)) % 256,\n" +
                "        r = n[a],\n" +
                "        n[a] = n[o],\n" +
                "        n[o] = r;\n" +
                "    for (var u = o = a = 0; u < t.length; u++)\n" +
                "      o = (o + n[a = (a + 1) % 256]) % 256,\n" +
                "        r = n[a],\n" +
                "        n[a] = n[o],\n" +
                "        n[o] = r,\n" +
                "        i += String.fromCharCode(t.charCodeAt(u) ^ n[(n[a] + n[o]) % 256]);\n" +
                "    return i\n" +
                "  }(function (e, t) {\n" +
                "    var r = 0\n" +
                "      , n = 0\n" +
                "      , o = []\n" +
                "      , i = 0;\n" +
                "    for (o = [],\n" +
                "           i = 0; i < e.length; i++) {\n" +
                "      for (r = 0,\n" +
                "             r = e[i] >= \"a\" && e[i] <= \"z\" ? e[i].charCodeAt(0) - 97 : e[i] - 0 + 26,\n" +
                "             n = 0; n < 36; n++)\n" +
                "        if (t[n] == r) {\n" +
                "          r = n;\n" +
                "          break\n" +
                "        }\n" +
                "      o[i] = r > 25 ? r - 26 : String.fromCharCode(r + 97)\n" +
                "    }\n" +
                "    return o.join(\"\")\n" +
                "  }(\"dg3utf1k6yxdwi09\", a), function (e) {\n" +
                "    if (!e)\n" +
                "      return \"\";\n" +
                "    var t, r, n, o, i,a = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1];\n" +
                "    for (o = (e = e.toString()).length,\n" +
                "           n = 0,\n" +
                "           i = \"\"; n < o;) {\n" +
                "      do {\n" +
                "        t = a[255 & e.charCodeAt(n++)]\n" +
                "      } while (n < o && -1 == t);\n" +
                "      if (-1 == t)\n" +
                "        break;\n" +
                "      do {\n" +
                "        r = a[255 & e.charCodeAt(n++)]\n" +
                "      } while (n < o && -1 == r);\n" +
                "      if (-1 == r)\n" +
                "        break;\n" +
                "      i += String.fromCharCode(t << 2 | (48 & r) >> 4);\n" +
                "      do {\n" +
                "        if (61 == (t = 255 & e.charCodeAt(n++)))\n" +
                "          return i;\n" +
                "        t = a[t]\n" +
                "      } while (n < o && -1 == t);\n" +
                "      if (-1 == t)\n" +
                "        break;\n" +
                "      i += String.fromCharCode((15 & r) << 4 | (60 & t) >> 2);\n" +
                "      do {\n" +
                "        if (61 == (r = 255 & e.charCodeAt(n++)))\n" +
                "          return i;\n" +
                "        r = a[r]\n" +
                "      } while (n < o && -1 == r);\n" +
                "      if (-1 == r)\n" +
                "        break;\n" +
                "      i += String.fromCharCode((3 & t) << 6 | r)\n" +
                "    }\n" +
                "    return i\n" +
                "  }(e)).split(\"-\"), 4)\n" +
                "    , r = t[0];\n" +
                "  return {\n" +
                "    sign: t[1],\n" +
                "    buy_key: r,\n" +
                "    token: t[2],\n" +
                "    timestamp: t[3]\n" +
                "  }\n" +
                "}\n" +
                "var a = getEncryptedFileParams('"+ep+"');\n" +
                "\n" +
                "\n" +
                "\n";
        Object eval = engine.eval(str);
        ScriptObjectMirror a = (ScriptObjectMirror)engine.get("a");
        String sign = StrUtil.toString(a.get("sign"));
        String buy_key = StrUtil.toString(a.get("buy_key"));
        String token = StrUtil.toString(a.get("token"));
        String timestamp = StrUtil.toString(a.get("timestamp"));
        return StrUtil.format("?sign={}&buy_key={}&token={}&timestamp={}",sign,buy_key,token,timestamp);
    }



}
